function outdir = ratheartpointselect( studydir )
% Click 5 points on the myocardium.

%
% Usage: 
%   outdir = ratheartpointselect( studydir )
% Where:
%   studydir = a directory 'T2', on which ratheartT2fit was run before.
%   outdir = the directory with output results.
%
% Output: this function save a file points.txt, for use in elastix,
% and a file points.mat, for later use by matlab. Both are saved in the
% T2postproc directory (=outdir).
%
% Based on Gabrielle's SelectionOfPoints.m, adapted for ratheart/repro test

% size of figure for manual segmentation:
figuresize = [100 100 1000 800];

% remove slashes at the end
while ( studydir(end) == '/' || studydir(end) == '\' )
    studydir = studydir(1:(end-1));
end

outdir = [ studydir 'postproc\' ];
if ~exist(outdir, 'dir')
  error( 'ratheartpointselect:invalidoutputdir', [ outdir ': invalid postproc dir. Run first ratheartT2fit!' ]);  
end

info=dicominfo( fullfile(outdir, 'FirstIm.dcm') );
original_img= double(dicomread(info));

figure( 'position', figuresize );
imshow(original_img, [min(original_img(:)), max(original_img(:))]);
title( 'click 5 points in the right order (see thesis Gabrielle)' );
points = ginput(5);
points = round(points);
close;
pointselastix = points - 1; %matlab starts counting at 1 and elastix at 0.

%creation of the text file that stores the selected points
fid = fopen( fullfile(outdir, 'points.txt'),'wt');
fprintf(fid,'%s\n%i\n','index',5);
fprintf(fid,'%f %f\n',pointselastix.');
fclose(fid);

% save in matlab format for definition of zero plane
save( fullfile(outdir, 'points.mat'), 'points', 'pointselastix');
